import axios from 'axios'
import { Message} from 'element-ui';

function getDownload(url,params = {}){
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
      params:params,
      headers: {
        Accept: 'application/octet-stream',     
      },
    }).then( res => {
      // const contentDisposition = res.headers['content-disposition']; 
      // const fileName = decodeURI(contentDisposition.split(';')[1].split('=')[1]);
      // let blob = new Blob([res.data], { type: 'application/vnd.ms-excel'});
      // const link = document.createElement('a');
      // link.href = URL.createObjectURL(blob); 
      // link.download = fileName;
      // link.style.display = 'none';
      // document.body.appendChild(link);
      // link.click();
      // URL.revokeObjectURL(link.href);
      // document.body.removeChild(link);
      resolve(res)
    }).catch(err => {
      Message.error(err);
      reject(err);
    })
  })
}

function postDownload(url,params = {}){
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: url,
      responseType: 'blob',
      data:params,
      headers: {
        Accept: 'application/octet-stream',     
      },
    }).then( res => {
      resolve(res)
    }).catch(err => {
      Message.error(err);
      reject(err);
    })
  })
}



export{
  getDownload,
  postDownload
}